<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Databas gate
 */
class Database {
	private static $_instance = null;
	private $_driver = null;

    public function __clone() {
        throw new Node_Exception('Can`t clone database object');
    }

    private static function &instance() {
        if (!isset(self::$_instance)) {
            $c = __CLASS__;
            self::$_instance = new $c;
        }

        return self::$_instance;
    }

    public static function &factory() {
        return self::driver();
    }
	
	public static function execute($sql) {
        return self::query($sql);
    }

	public static function query($sql) {
        return self::instance()->_driver->query($sql);
    }

    public static function escape($value) {
        return self::instance()->_driver->escape($value);
    }

    public static function &driver() {
        return self::instance()->_driver;
    }

	/**
	 * Constructor
	 */
	private function __construct() {
		if (!$this->_driver) {
			$driver = ucfirst(trim(strtolower(Config::get('driver', 'database'))));
			$class_name = 'Database_Driver_' . $driver;
			$this->_driver = new $class_name;
		}
	}
}
